// pages/sign/sign.js
var db_res = {};
wx.cloud.init({
  env: 'cloud1-7g5ffdpz146dadd4'
});
const db = wx.cloud.database();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    temp:0,
    canUseGetUserProfile: false,
    openid:'',
    wx_name:'',
    wx_image:'',
  },
  //管理人员跳转至登录界面;
  mclick: function(){
    wx.navigateTo({
      url: '/pages/manage/manage',
    })
  },
  //求职者获取用户信息逻辑;
  iclick:function(e){
    if(this.data.temp===0)
    { 
      return new Promise (
        resolve=>{
          wx.getUserProfile({
            desc:'获取用户的信息',
            fail:res=>{},
            success:(res)=>{
              var that = this;
              db.collection('users_info').add({
                data:{
                openid: that.data.openid,
                wx_name:res.userInfo.nickName,
                wx_image:res.userInfo.avatarUrl,
                }
              }).then(res=>
                {
                    wx.hideLoading();
                    wx.showToast({
                        title: '注册成功',
                        icon: 'success',
                        mask: true,
                        complete: () => {
                            setTimeout(() => {
                                wx.hideToast();
                                wx.redirectTo({
                                    url: '/pages/index/index',
                                });
                            }, 1200)
                        }
                    });
                })
            }

          })
          resolve();
        }
      )
    }
    else {
    wx.navigateTo({
      url: '/pages/index/index',
    })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */ 
  onLoad: async function(options) { 
    var that = this;
    if (wx.getUserProfile) {
      this.setData({
         canUseGetUserProfile: true
      })
     }
    await wx.cloud.callFunction({
        name: 'getOpenid',
        data: {}
    }).then(res => {
        that.setData({
            openid: res.result.openid
        });
        db.collection('users_info').where({
        openid: res.result.openid
    }).count().then(res => {
      if (res.total > 0) {
          this.setData({
              temp:1
          }) 
      } 
  });
});
wx.setStorageSync('openid', this.data.openid)
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  
  }, 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  }, 

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})